package com.hzf.config.admin.controller.manage;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.hzf.common.base.BaseController;
import com.hzf.common.util.MD5Util;
import com.hzf.common.util.StringUtil;
import com.hzf.common.validator.LengthValidator;
import com.hzf.common.validator.NotNullValidator;
import com.hzf.config.dao.model.ConfigRegion;
import com.hzf.config.dao.model.ConfigRegionExample;
import com.hzf.config.rpc.api.ConfigRegionService;
import com.hzf.config.rpc.common.constant.ConfigResult;
import com.hzf.config.rpc.common.constant.ConfigResultConstant;
import com.hzf.upms.common.constant.UpmsResult;
import com.hzf.upms.common.constant.UpmsResultConstant;
import com.hzf.upms.dao.model.UpmsUser;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/manage/region")
public class ConfigRegionController extends BaseController {
    private static Logger _log = LoggerFactory.getLogger(ConfigRegionController.class);

    @Autowired
    private ConfigRegionService configRegionService;
    @RequestMapping(value="/index",method = RequestMethod.GET)
    @RequiresPermissions("config:region:read")
    @ApiOperation(value = "地区设置首页")
    public String index(){
        return "/manage/region/index.jsp";
    }
    @RequestMapping(value="/list",method = RequestMethod.GET)
    @ApiOperation(value = "地区设置列表")
    @RequiresPermissions("config:region:read")
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        ConfigRegionExample configRegionExample = new ConfigRegionExample();
        if(!StringUtils.isBlank(sort)&&!StringUtils.isBlank(order)){
            configRegionExample.setOrderByClause(StringUtil.humpToLine(sort) + " " + order);
        }
        List<ConfigRegion> rows = configRegionService.selectByExampleForOffsetPage(configRegionExample,offset,limit);
        long total = configRegionService.countByExample(configRegionExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }
    @ApiOperation(value = "新增地区")
    @RequiresPermissions("config:region:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Model model) {
        ConfigRegionExample configRegionExample = new ConfigRegionExample();
        configRegionExample.createCriteria().andRegionTypeEqualTo(new Long((long)0));
        List<ConfigRegion> rows = configRegionService.selectByExample(configRegionExample);
        model.addAttribute("types",rows);
        return "/manage/region/create.jsp";
    }
    @ApiOperation(value = "新增地区")
    @RequiresPermissions("config:region:create")
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Object create(ConfigRegion configRegion) {
        ComplexResult result = FluentValidator.checkAll()
                .on(configRegion.getRegionName(), new LengthValidator(1, 20, "地区名"))
                .on(configRegion.getRegionName(), new NotNullValidator("姓名"))
                .on(configRegion.getRegionNumber().toString(),new NotNullValidator("编号"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new ConfigResult(ConfigResultConstant.INVALID_LENGTH, result.getErrors());
        }

        if(configRegion.getSuperiorId()==0){
            configRegion.setRegionType(new Long((long)0));
        }else {
            configRegion.setRegionType(new Long((long)1));
        }
        int count = configRegionService.insert(configRegion);

        return new ConfigResult(ConfigResultConstant.SUCCESS, count);
    }
    @ApiOperation(value = "删除地区")
    @RequiresPermissions("config:region:delete")
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = configRegionService.deleteByPrimaryKeys(ids);
        return new ConfigResult(ConfigResultConstant.SUCCESS, count);
    }
    @ApiOperation(value = "修改地区")
    @RequiresPermissions("config:region:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        ConfigRegion configRegion = configRegionService.selectByPrimaryKey(id);
        modelMap.put("configRegion", configRegion);
        System.out.println("返回了");
        return "/manage/region/update.jsp";
    }

    @ApiOperation(value = "修改用户")
    @RequiresPermissions("config:region:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") Integer id, ConfigRegion configRegion) {
        ComplexResult result = FluentValidator.checkAll()
                .on(configRegion.getRegionName(), new LengthValidator(1, 20, "地区名"))
                .on(configRegion.getRegionName(), new NotNullValidator("姓名"))
                .on(configRegion.getRegionNumber().toString(),new NotNullValidator("编号"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new ConfigResult(ConfigResultConstant.INVALID_LENGTH, result.getErrors());
        }
        configRegion.setRegionId(id);
        if(configRegion.getSuperiorId()==0){
            configRegion.setRegionType(new Long((long)0));
        }else {
            configRegion.setRegionType(new Long((long)1));
        }
        int count = configRegionService.updateByPrimaryKeySelective(configRegion);
        return new ConfigResult(ConfigResultConstant.SUCCESS, count);
    }
}
